package com.server.db.bean;

import com.sandking.tools.SK_Map;
import java.util.ArrayList;
import com.server.db.imp.YjfjImp;
import com.alibaba.fastjson.JSON;
import java.util.Map;
import com.server.db.imp.YhImp;
import com.sandking.io.SK_ByteBuff;
import com.sandking.tools.SK_Plus;
import java.util.List;
import java.util.HashMap;
import com.server.db.imp.YhyjImp;

/**
 * 用户邮件
 */
public class Yhyj {

	public static final String TABLE_NAME = "用户邮件";
	public static final String CLASS_NAME = "Yhyj"; 
	static final int MAX_GET = Short.MAX_VALUE;
	
	//索引名称
	public static final String  Id_IndexName = "id";
	public static final String  Fjrid_IndexName = "fjrid";
	public static final String  Jsrid_IndexName = "jsrid";
	
	//对象版本号	
	private int _v = 0;
	public int _nextVer(){
		if(_v > MAX_GET) _v = 0;
		return ++_v;
	}
	
	public int _getVer(){
		return _v;
	}
	
	//是否是源对象
	private boolean _source = false;
	
	public boolean is_source() {
		return _source;
	}

	public void set_source(boolean _source) {
		this._source = _source;
	}
	
	//Cache 中的索引
	private String  id_Unique = null;
	private String  fjrid_Index = null;
	private String  jsrid_Index = null;
	public Yhyj updatePrimaryIndex(){
		return updatePrimaryIndex(this);
	}
	
	public static List<Yhyj> updatePrimaryIndex(List<Yhyj> yhyjs){
		for(Yhyj yhyj : yhyjs){
			updatePrimaryIndex(yhyj);	
		}
		return yhyjs;
	}
	
	public static Yhyj updatePrimaryIndex(Yhyj yhyj){
		String field = SK_Plus.b(yhyj.getId()).e();
		yhyj.setId_Unique(field);
		
		field = SK_Plus.b(yhyj.getFjrid()).e();
		yhyj.setFjrid_Index(field);
		
		field = SK_Plus.b(yhyj.getJsrid()).e();
		yhyj.setJsrid_Index(field);
		
		return yhyj;
	}
	
	
	/**
	 * 需要更新字段集
	 */
	private Map<String, Object> updateColumns;

	/* 可能需要补救的字段 */
	private SK_ByteBuff byteBuff = SK_ByteBuff.allocate();
	
	public SK_ByteBuff getSK_ByteBuff(){
		return this.byteBuff;
	}
	
	/** id */
	private int id;
	
	/** 创建时间 */
	private java.util.Date cjsj;
	
	/** 读取时间 */
	private java.util.Date dqsj;
	
	/** 是否读取 */
	private java.util.Date sfdq;
	
	/** 发件人id */
	private int fjrid;
	
	/** 接收人id */
	private int jsrid;
	
	
	public Yhyj() {
		super();
		this._source = true;
	}
	
	public Yhyj(int id, java.util.Date cjsj, java.util.Date dqsj, java.util.Date sfdq, int fjrid, int jsrid) {
		super();
		this.id = id;
		this.cjsj = cjsj;
		this.dqsj = dqsj;
		this.sfdq = sfdq;
		this.fjrid = fjrid;
		this.jsrid = jsrid;
		this._source = true;
	}
	
	public Map<String, Object> getUpdateColumns() {
		if(updateColumns == null)
			updateColumns = new HashMap<String, Object>();
		return updateColumns;
	}
	
	public void setUpdateColumns(Map<String, Object> updateColumns) {
		this.updateColumns = updateColumns;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		changeSetId(id);
		if(!this._source){
			byteBuff.writeInt(1);
			byteBuff.writeInt(id);
		}
	}
	
	private void changeSetId(int id) {
		this.id = id;
		if(!this._source){
			addUpdateColumn("id",id);
		}
	}
	
	public void changeIdWith(int id){
		this.id += id;
		changeSetId(this.id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(2);
			byteBuff.writeInt(id);
		}
	}
	public void changeIdWithMin(int id,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		changeSetId(this.id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(3);
			byteBuff.writeInt(id);
			byteBuff.writeInt(min);
		}	
	}
	public void changeIdWithMax(int id,int max){
		this.id += id;
		this.id = this.id > max ? max : this.id;
		changeSetId(this.id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(4);
			byteBuff.writeInt(id);
			byteBuff.writeInt(max);
		}
	}
	public void changeIdWithMaxMin(int id,int max,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		this.id = this.id > max ? max : this.id;
		changeSetId(this.id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(5);
			byteBuff.writeInt(id);
			byteBuff.writeInt(max);
			byteBuff.writeInt(min);
		}	
	}	
	public java.util.Date getCjsj() {
		return cjsj;
	}
	
	public void setCjsj(java.util.Date cjsj) {
		changeSetCjsj(cjsj);
		if(!this._source){
			byteBuff.writeInt(6);
			byteBuff.writeDate(cjsj);
		}
	}
	
	private void changeSetCjsj(java.util.Date cjsj) {
		this.cjsj = cjsj;
		if(!this._source){
			addUpdateColumn("创建时间",cjsj);
		}
	}
	
	public java.util.Date getDqsj() {
		return dqsj;
	}
	
	public void setDqsj(java.util.Date dqsj) {
		changeSetDqsj(dqsj);
		if(!this._source){
			byteBuff.writeInt(7);
			byteBuff.writeDate(dqsj);
		}
	}
	
	private void changeSetDqsj(java.util.Date dqsj) {
		this.dqsj = dqsj;
		if(!this._source){
			addUpdateColumn("读取时间",dqsj);
		}
	}
	
	public java.util.Date getSfdq() {
		return sfdq;
	}
	
	public void setSfdq(java.util.Date sfdq) {
		changeSetSfdq(sfdq);
		if(!this._source){
			byteBuff.writeInt(8);
			byteBuff.writeDate(sfdq);
		}
	}
	
	private void changeSetSfdq(java.util.Date sfdq) {
		this.sfdq = sfdq;
		if(!this._source){
			addUpdateColumn("是否读取",sfdq);
		}
	}
	
	public int getFjrid() {
		return fjrid;
	}
	
	public void setFjrid(int fjrid) {
		changeSetFjrid(fjrid);
		if(!this._source){
			byteBuff.writeInt(9);
			byteBuff.writeInt(fjrid);
		}
	}
	
	private void changeSetFjrid(int fjrid) {
		this.fjrid = fjrid;
		if(!this._source){
			addUpdateColumn("发件人id",fjrid);
		}
	}
	
	public void changeFjridWith(int fjrid){
		this.fjrid += fjrid;
		changeSetFjrid(this.fjrid);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(10);
			byteBuff.writeInt(fjrid);
		}
	}
	public void changeFjridWithMin(int fjrid,int min){
		this.fjrid += fjrid;
		this.fjrid = this.fjrid < min ? min : this.fjrid;
		changeSetFjrid(this.fjrid);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(11);
			byteBuff.writeInt(fjrid);
			byteBuff.writeInt(min);
		}	
	}
	public void changeFjridWithMax(int fjrid,int max){
		this.fjrid += fjrid;
		this.fjrid = this.fjrid > max ? max : this.fjrid;
		changeSetFjrid(this.fjrid);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(12);
			byteBuff.writeInt(fjrid);
			byteBuff.writeInt(max);
		}
	}
	public void changeFjridWithMaxMin(int fjrid,int max,int min){
		this.fjrid += fjrid;
		this.fjrid = this.fjrid < min ? min : this.fjrid;
		this.fjrid = this.fjrid > max ? max : this.fjrid;
		changeSetFjrid(this.fjrid);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(13);
			byteBuff.writeInt(fjrid);
			byteBuff.writeInt(max);
			byteBuff.writeInt(min);
		}	
	}	
	public int getJsrid() {
		return jsrid;
	}
	
	public void setJsrid(int jsrid) {
		changeSetJsrid(jsrid);
		if(!this._source){
			byteBuff.writeInt(14);
			byteBuff.writeInt(jsrid);
		}
	}
	
	private void changeSetJsrid(int jsrid) {
		this.jsrid = jsrid;
		if(!this._source){
			addUpdateColumn("接收人id",jsrid);
		}
	}
	
	public void changeJsridWith(int jsrid){
		this.jsrid += jsrid;
		changeSetJsrid(this.jsrid);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(15);
			byteBuff.writeInt(jsrid);
		}
	}
	public void changeJsridWithMin(int jsrid,int min){
		this.jsrid += jsrid;
		this.jsrid = this.jsrid < min ? min : this.jsrid;
		changeSetJsrid(this.jsrid);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(16);
			byteBuff.writeInt(jsrid);
			byteBuff.writeInt(min);
		}	
	}
	public void changeJsridWithMax(int jsrid,int max){
		this.jsrid += jsrid;
		this.jsrid = this.jsrid > max ? max : this.jsrid;
		changeSetJsrid(this.jsrid);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(17);
			byteBuff.writeInt(jsrid);
			byteBuff.writeInt(max);
		}
	}
	public void changeJsridWithMaxMin(int jsrid,int max,int min){
		this.jsrid += jsrid;
		this.jsrid = this.jsrid < min ? min : this.jsrid;
		this.jsrid = this.jsrid > max ? max : this.jsrid;
		changeSetJsrid(this.jsrid);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(18);
			byteBuff.writeInt(jsrid);
			byteBuff.writeInt(max);
			byteBuff.writeInt(min);
		}	
	}	
	
	//id
	public List<Yjfj> getYjfjsFkYhyj_id(){
		return YjfjImp.getByYhyj_id(id);
	}
	
	//发件人id
	public Yh getYhPkFjrid(){
		return YhImp.getById(fjrid);
	}
	//接收人id
	public Yh getYhPkJsrid(){
		return YhImp.getById(jsrid);
	}
	public void addUpdateColumn(String key, Object val) {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.put(key, val);
	}
	/**
	public void clearUpdateColumn() {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.clear();
	}
	*/
	public Yhyj clone(){
		Yhyj yhyj = new Yhyj();
		yhyj.setId(this.getId());
		yhyj.setCjsj(this.getCjsj());
		yhyj.setDqsj(this.getDqsj());
		yhyj.setSfdq(this.getSfdq());
		yhyj.setFjrid(this.getFjrid());
		yhyj.setJsrid(this.getJsrid());
		yhyj._v = _v;
		yhyj._source = false;
		return yhyj.updatePrimaryIndex(); 
	}
	
	public Map<String, Object> toMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("cjsj", this.cjsj);
        result.put("dqsj", this.dqsj);
        result.put("sfdq", this.sfdq);
        result.put("fjrid", this.fjrid);
        result.put("jsrid", this.jsrid);
        return result;
    }
    
    public String toString(){
        return toMap().toString();
    }
    
    public String toJson(){
    	return JSON.toJSONString(toMap());
    }
    
    /**
     * 数据库源字段Map
     */
    public Map<String, Object> toColumnNameMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("创建时间", this.cjsj);
        result.put("读取时间", this.dqsj);
        result.put("是否读取", this.sfdq);
        result.put("发件人id", this.fjrid);
        result.put("接收人id", this.jsrid);
        return result;
    }
    
    public String toColumnNameString(){
        return toColumnNameMap().toString();
    }
    
    public byte[] toBytes() throws Exception {
    	SK_ByteBuff byteBuff = SK_ByteBuff.allocate();
	    byteBuff.writeInt(this.id);
	    byteBuff.writeDate(this.cjsj);
	    byteBuff.writeDate(this.dqsj);
	    byteBuff.writeDate(this.sfdq);
	    byteBuff.writeInt(this.fjrid);
	    byteBuff.writeInt(this.jsrid);
	    return byteBuff.toByte();
    }
    
    public byte[] toSKBytes(){
    	SK_ByteBuff byteBuff = SK_ByteBuff.allocate();
    	byteBuff.writeMap(toMap());
	    return byteBuff.toByte();
    }
    
     public static Yhyj createForBytes(byte[] _byte) throws Exception {
     	SK_ByteBuff byteBuff = SK_ByteBuff.wrap(_byte);
     	Yhyj yhyj = new Yhyj();
	    yhyj.id = byteBuff.readInt(null);
	    yhyj.cjsj = byteBuff.readDate(null);
	    yhyj.dqsj = byteBuff.readDate(null);
	    yhyj.sfdq = byteBuff.readDate(null);
	    yhyj.fjrid = byteBuff.readInt(null);
	    yhyj.jsrid = byteBuff.readInt(null);
	    return yhyj.updatePrimaryIndex();
     }
     
     public static Yhyj createForSKBytes(byte[] _byte){
     		SK_ByteBuff byteBuff = SK_ByteBuff.wrap(_byte);
     		@SuppressWarnings("unchecked")
     		Map<String,Object> map = byteBuff.readMap(null);
	     	Yhyj yhyj = Yhyj.createForMap(map);
		    return yhyj.updatePrimaryIndex();
     }
    
    /**
     * 根据list创建对象
     */
    public static List<Yhyj> createForColumnNameList(List<Map<String, Object>> list){
    	List<Yhyj> yhyjs = new ArrayList<Yhyj>();
		for (Map<String, Object> map : list) {
			yhyjs.add(createForColumnNameMap(map));
		}
		return yhyjs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Yhyj createForColumnNameMap(Map<String, Object> map){
    	Yhyj obj = new Yhyj();
	    obj.id = SK_Map.getInt("id", map);
	    obj.cjsj = SK_Map.getDate("创建时间", map);
	    obj.dqsj = SK_Map.getDate("读取时间", map);
	    obj.sfdq = SK_Map.getDate("是否读取", map);
	    obj.fjrid = SK_Map.getInt("发件人id", map);
	    obj.jsrid = SK_Map.getInt("接收人id", map);
        return obj.updatePrimaryIndex();
    }
    
    /**
     * 根据list创建对象
     */
    public static List<Yhyj> createForList(List<Map<String, Object>> list){
    	List<Yhyj> yhyjs = new ArrayList<Yhyj>();
		for (Map<String, Object> map : list) {
			yhyjs.add(createForColumnNameMap(map));
		}
		return yhyjs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Yhyj createForMap(Map<String, Object> map){
    	Yhyj obj = new Yhyj();
	    obj.id = SK_Map.getInt("id", map);
	    obj.cjsj = SK_Map.getDate("cjsj", map);
	    obj.dqsj = SK_Map.getDate("dqsj", map);
	    obj.sfdq = SK_Map.getDate("sfdq", map);
	    obj.fjrid = SK_Map.getInt("fjrid", map);
	    obj.jsrid = SK_Map.getInt("jsrid", map);
        return obj.updatePrimaryIndex();
    }
    
    public static Yhyj createForJson(String json){
    	Map<String, Object> map = JSON.parseObject(json);
    	return createForMap(map);
    }
    
    public static List<Yhyj> createForJson(List<String> jsons){
    	List<Yhyj> yhyjs = new ArrayList<Yhyj>();
    	for(String json : jsons){
    		yhyjs.add(createForJson(json));
    	}
    	return yhyjs;
    }
    
    /** 级联删除(延迟入库) */
    public boolean deleteAndSon(){
    	return false;
    }
    
    /** 级联删除(及时入库) */
    public boolean deleteAndSonAndFlush(){
    	return false;
    }
    
    /** 延迟插入数据库 */
    public Yhyj insert(){
    	return YhyjImp.insert(this);
    }
    /** 延迟更新数据库 */
    public Yhyj update(){
    	return YhyjImp.update(this);
    }
    /** 延迟删除数据库 */
    public boolean delete(){
    	return YhyjImp.delete(this);
    }
    /** 即时插入数据库 */
    public Yhyj insertAndFlush(){
    	return YhyjImp.insertAndFlush(this);
    }
    /** 即时更新数据库 */
    public Yhyj updateAndFlush(){
    	return YhyjImp.updateAndFlush(this);
    }
    /** 即时删除数据库 */
    public boolean deleteAndFlush(){
    	return YhyjImp.deleteAndFlush(this);
    }
    
    
    
    //--------------------------------Cache 中的索引 GET & SET
	public String getId_Unique(){
		return id_Unique;
	}
	
	public void setId_Unique(String id_Unique){
		this.id_Unique = id_Unique;
	}
	
	public String getFjrid_Index(){
		return fjrid_Index;
	}
	
	public void setFjrid_Index(String fjrid_Index){
		this.fjrid_Index = fjrid_Index;
	}
	
	public String getJsrid_Index(){
		return jsrid_Index;
	}
	
	public void setJsrid_Index(String jsrid_Index){
		this.jsrid_Index = jsrid_Index;
	}
	
	
	
	/** 取得改变的索引 */
	public Map<String,String> getUpdateIndexs(){
		Map<String,String> indexNames = new HashMap<String,String>();
		if(!this.getId_Unique().equals(SK_Plus.b(id).e())){
			indexNames.put(Yhyj.Id_IndexName,SK_Plus.b(id).e());
			indexNames.put("isUpdtaeUnique","true");
		}
			
		if(!this.getFjrid_Index().equals(SK_Plus.b(fjrid).e()))
			indexNames.put(Yhyj.Fjrid_IndexName,SK_Plus.b(fjrid).e());
			
		if(!this.getJsrid_Index().equals(SK_Plus.b(jsrid).e()))
			indexNames.put(Yhyj.Jsrid_IndexName,SK_Plus.b(jsrid).e());
			
		return indexNames;
	}
}